<?php

namespace App\Http\Controllers\Lst;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;

use App\Repositories\Eloquent\Lst\StudentRepository as Student;
use App\Repositories\Eloquent\Edu\TypeRepository as Type;
use App\Repositories\Eloquent\Edu\TermRepository as Term;
use App\Repositories\Eloquent\Edu\CategoryRepository as Category;
use App\Repositories\Eloquent\Edu\ClaseRepository as Clase;
use App\Repositories\Eloquent\Lst\PublishRepository as Publish;

use App\Exports\PublishExport;
use Maatwebsite\Excel\Facades\Excel;

class StudentController extends Controller
{

	protected $type, $student, $term, $category, $clase, $publish;

    public function __construct(Student $student, Type $type,
                Term $term, Category $category, Clase $clase, Publish $publish)
    {
        parent::__construct();

        $this->student = $student;

        $this->type = $type;

        $this->term = $term;

        $this->category = $category;

        $this->clase = $clase;

        $this->publish = $publish;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function shows(Request $request,$publishId,$claseId)
    {
        $students = $this->student->lists($request,$publishId,$claseId);

        $publish = $this->publish->show($publishId);

        $clase = $this->clase->show($claseId);

        $paging = round(count($students)/2);

        return view($this->view, compact('students','publish','clase','paging'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {

    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {

    }


    /**
     * 发布方案列表
     * @return [type] [description]
     */
    public function publish()
    {
        $publishs = $this->publish->lists();

        return view($this->view, compact('publishs'));
    }


    /**
     * 新建发布方案
     * @return [type] [description]
     */
    public function publishCreate()
    {
        $terms = $this->term->publish();

        return view($this->view, compact('terms'));
    }


    /**
     * 保存发布方案
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function publishStore(Request $request)
    {
        return $this->publish->store($request);
    }


    /**
     * 修改发布方案
     * @param  Request $request [description]
     * @param  [type]  $id      [description]
     * @return [type]           [description]
     */
    public function publishEdit(Request $request, $id)
    {
        $publish = $this->publish->show($id);

        if ($publish instanceof JsonResponse && $publish->getStatusCode() === 404) {
            return abort('404');
        }

        return view($this->view, compact('publish'));
    }


    /**
     * 更新发布方案
     * @param  Request $request [description]
     * @param  [type]  $id      [description]
     * @return [type]           [description]
     */
    public function publishUpdate(Request $request,$id)
    {
        return $this->publish->renew($request, $id);
    }

    /**
     * 更改发布方案状态
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function publishChange(Request $request)
    {
        return $this->publish->change($request);
    }


    public function export($pid,$cid)
    {
        $publish = $this->publish->show($pid);

        $clase = $this->clase->show($cid);

        return (new PublishExport($pid, $cid))->download($publish->name.'_'.$clase->category->name.'_'.$clase->name.'花名册.xlsx');
    }

}
